package com.ltx.vizflowservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ltx.vizflowservice.common.convention.result.Result;
import com.ltx.vizflowservice.common.convention.result.Results;
import com.ltx.vizflowservice.domain.dto.ProjectDTO;
import com.ltx.vizflowservice.domain.entity.Project;
import com.ltx.vizflowservice.domain.entity.ProjectConfig;
import com.ltx.vizflowservice.domain.entity.ProjectTemplate;
import com.ltx.vizflowservice.domain.entity.User;
import com.ltx.vizflowservice.service.ProjectConfigService;
import com.ltx.vizflowservice.service.ProjectService;
import com.ltx.vizflowservice.util.UserContext;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;

/**
 * <p>
 * 项目表 前端控制器
 * </p>
 *
 * @author 罗添煦
 * @since 2024-11-15
 */
@RestController
@Slf4j
public class ProjectController {

    @Resource
    private ProjectService projectService;

    @Resource
    private ProjectConfigService projectConfigService;

    /**
     * 发布项目
     * @param projectDTO 项目信息
     * @return 项目发布结果
     */
    @PostMapping("/publishProject")
    public Result<?> publishProject(@RequestBody ProjectDTO projectDTO) {
        User user = UserContext.getUser();
        log.info("当前用户:{},发布项目内容projectDTO: {}", user.getUserName(), projectDTO);
        return projectService.createProject(projectDTO, user.getUserId() ,1);
    }

    /**
     * 发布模板
     * @param projectId 项目id
     * @return 发布结果
     */
    @GetMapping("/publishTemplate")
    public Result<?> publishTemplate(@RequestParam("projectId")Long projectId) {
        return projectService.publishTemplate(projectId);
    }

    /**
     * 获取项目列表
     * @return 项目列表
     */
    @GetMapping("/getProjectList")
    public Result<?> getProjectList() {
        return projectService.getProjectList();
    }

    /**
     * 获取模板列表
     * @return 模板列表
     */
    @GetMapping("/getTemplates")
    public Result<?> getTemplates(@RequestParam("currentNum")Integer currentNum,@RequestParam("pageSize")Integer pageSize) {
        // 分页查询
        Page<ProjectTemplate> page = new Page<>();
        page.setCurrent(currentNum);
        page.setSize(pageSize);
        return projectService.getTemplates(page);
    }

    /**
     * 使用模板
     * @param projectId 模板id
     * @return
     */
    @PostMapping("/useTemplate")
    public Result<?> useTemplate(@RequestParam("projectId") Long projectId) {
        log.info("使用模板projectId: {}", projectId);
        return projectService.useTemplate(projectId);
    }

    /**
     * 获取项目详情
     * @param projectId 项目id
     * @return 项目详情
     */
    @GetMapping("/getProjectDetails")
    public Result<?> getProjectDetails(@RequestParam("projectId") Integer projectId) {
        return projectService.getProjectDetails(projectId);
    }

    /**
     * 更新项目状态
     * @param projectId     项目id
     * @param projectStatus 项目状态
     * @return 更新结果
     */
    @GetMapping("/updateProjectStatus")
    public Result<?> updateProjectStatus(@RequestParam("projectId") Integer projectId, @RequestParam("projectStatus") Integer projectStatus) {
        return projectService.updateProjectStatus(projectId, projectStatus);
    }

    /**
     * 删除项目
     * @param projectId 项目id
     * @return 删除结果
     */
    @DeleteMapping("/deleteProject")
    public Result<?> deleteProject(@RequestParam("projectId") Integer projectId) {
        return Results.success(projectService.remove(
                Wrappers.lambdaQuery(Project.class)
                        .eq(Project::getProjectId, projectId)));
    }

    /**
     * 保存项目
     * @param projectDTO 项目信息
     * @return 保存结果
     */
    @PostMapping("/saveProject")
    public Result<?> saveProject(@RequestBody ProjectDTO projectDTO) {
        return projectService.saveProject(projectDTO);
    }

    /**
     * 获取项目配置
     * @return 项目配置
     */
    @GetMapping("/settings/canvas")
    public Result<?> getCanvasSize() {
        ProjectConfig projectConfig = projectConfigService.getOne(Wrappers.lambdaQuery(ProjectConfig.class)
                .eq(ProjectConfig::getUserId, UserContext.getUser().getUserId()));
        if (projectConfig == null){
            ProjectConfig projectConfig1 = new ProjectConfig();
            projectConfig1.setUserId(UserContext.getUser().getUserId());
            projectConfig1.setCanvasWidth(1920);
            projectConfig1.setCanvasHeight(1080);
            projectConfigService.save(projectConfig1);
            return Results.success(projectConfig1);
        }
        return Results.success(projectConfig);
    }

    /**
     * 设置项目配置
     * @param projectDTO 项目信息
     * @return 设置结果
     */
    @PostMapping("/settings")
    public Result<?> settings(@RequestBody ProjectDTO projectDTO) {
        LinkedHashMap<String,Integer> canvasSize = (LinkedHashMap<String, Integer>) projectDTO.getCanvasSize();
        projectConfigService.update(Wrappers.lambdaUpdate(ProjectConfig.class)
                .set(ProjectConfig::getCanvasHeight, canvasSize.get("canvasHeight"))
                .set(ProjectConfig::getCanvasWidth, canvasSize.get("canvasWidth"))
                .eq(ProjectConfig::getUserId, UserContext.getUser().getUserId()));
        return Results.success();
    }
}

